/*
 * @Descripttion: 
 * @version: 
 * @Author: Yaodecheng
 * @Date: 2019-10-19 10:45:26
 * @LastEditors: Yaodecheng
 * @LastEditTime: 2020-04-29 14:00:29
 */

#include "thread_base.h"

#ifdef _WIN32
DWORD WINAPI ThreadBase::threadfun(void *ptr)
#else
void *ThreadBase::threadfun(void *ptr)
#endif
{
#ifndef _WIN32
    pthread_detach(pthread_self());
#endif
    ThreadBase *p = (ThreadBase *)ptr;
    RunFun fun = p->m_fun;
    void *context = p->m_context;
    p->m_pLock.unlock();
    fun(context);
    return 0;
}

ThreadBase::ThreadBase(RunFun f, void *context)
    : m_fun(f), m_context(context)
{
    m_pLock.lock();
#ifdef _WIN32
    m_Thandle = CreateThread(NULL, 0, threadfun, this, 0, NULL);
    if (m_Thandle == NULL)
#else
    if (pthread_create(&m_recvThread, NULL, threadfun, this))
#endif
    {
        printf("Error creating readThread.\n");
        m_pLock.unlock();
    }
    m_pLock.lock();
#ifdef _WIN32
    CloseHandle(m_Thandle);
#endif
}

ThreadBase::~ThreadBase()
{
}
